package com.study.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.task.TaskExecutorBuilder;
import org.springframework.boot.task.TaskSchedulerBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.time.Duration;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Configuration
public class TaskExecutorConfig {

    @Bean("taskExecutor")
    public Executor getThreadPoolTaskExecutor() {
        log.info("THREAD POOL LOAD...");
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();

        taskExecutor.setCorePoolSize(8);
        taskExecutor.setMaxPoolSize(20);
        taskExecutor.setQueueCapacity(200);
        taskExecutor.setKeepAliveSeconds(60);
        taskExecutor.setAwaitTerminationSeconds(60);
        taskExecutor.setThreadNamePrefix("taskExecutor-");
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        taskExecutor.initialize();
        return taskExecutor;
    }


    @Bean("TaskExecutor")
    public Executor taskExecutorBuilder(){
        return new TaskExecutorBuilder()
                .corePoolSize(8)
                .maxPoolSize(16)
                .queueCapacity(100)
                .threadNamePrefix("TASK-EXECUTOR")
                .keepAlive(Duration.ofSeconds(10))
                .build();
    }

}
